import axios from 'axios';
import {Message,MessageBox} from 'element-ui';
import {objectMerge} from './index.js' ;
import lltMessage from '../components/common/alert/messageOnce';
import {getParamsUrl} from "./index.js";
import router from '../pages/index/router';

import store from '../store';
const config = {
	baseURL: process.env.BASE_API, // api的base_url
	timeout: 30000 // 请求超时时间
};

// new 对象实例
const message = new lltMessage();




const goLogin = (res)=> {
	message.warning({
		title: '提示',
		message: res.message,
		closeOnClickModal: false,
		type: 'warning',
		showClose: true,
		callback: action => {
			store.commit('LOGOUT_USER');
			if (action == 'confirm') {
				let param = getParamsUrl();
				router.push({
					path: '/login?path='+param.path,
					query: param.query
				});
			}
		}
	});
	return Promise.reject(res);
};

/***
 * 判断是否为
 * 中天易税云接口
 * whiteUrl白名单
 * @type {string}
 */
let isZtUrl = '';
let whiteUrl = [
	'https://127.0.0.1:51888?app=ztyun',
	'https://127.0.0.1:51888/callshell/?app=ztyun'
]
const interceptors = {
	request: {
		config: config => {
			isZtUrl = config.baseURL;
			return config;
		},
		error: error => {
			Promise.reject(error);
		}
	},
	response: {
		response: response => {
			console.log(response)
			const res = response.data;
			const noMessageBox = response.config.noMessageBox || false;
			//连连通公司接口
			if(!whiteUrl.includes(isZtUrl)) {
				if (res.code == 200) {
					return res;
				} else {
					if (res.code === 102) {
						return goLogin(res);
					}
					else if (res.code === 1001) {
						if (!noMessageBox)
							Message({
								message: "当前企业不是出口退税企业！",
								type: 'error',
								duration: 5 * 1000
							});
					} else {
						if (!noMessageBox) {
							if(res){
								Message({
									message: res.message,
									type: 'error',
									duration: 3 * 1000
								});
							}else{
								Message({
									message: "系统异常，请联系管理员！"+process.env.LLT_KF_PHONE,
									type: 'error',
									duration: 3 * 1000
								});
							}
						}
					}
					return Promise.reject(res);
				}
			}
			//中天易税云接口
			else{
				if(res.retCode!='000'){
					Message({
						message: res.retMessage,
						type: 'error',
						duration: 3 * 1000
					});
					return Promise.reject(res);
				}else{
					return res;
				}
			}
		},
		error: (err) => {
			let errMessage = '';
			if (err && err.response) {
				switch (err.response.status) {
					case 400:
						errMessage = '请求错误';
						break
					case 403:
						errMessage = '拒绝访问';
						break
					case 404:
						errMessage = `请求地址出错: ${err.response.config.url}`;
						break
					case 408:
						errMessage = '请求超时';
						break
					case 500:
						errMessage = '服务器内部错误';
						break
					case 501:
						errMessage = '服务未实现';
						break
					case 502:
						errMessage = '网关错误';
						break
					case 503:
						errMessage = '服务不可用';
						break
					case 504:
						errMessage = '网关超时';
						break
					case 505:
						errMessage = 'HTTP版本不受支持';
						break
					default:
				}
			} else {
				errMessage = '请检查网络';
			}
			if(!whiteUrl.includes(isZtUrl)){
				Message({
					message: errMessage,
					type: 'error',
					duration: 3 * 1000
				});
			}

			return Promise.reject(err);
		}
	}
}
const jsonService = axios.create(
	objectMerge(
		{
			headers: {'Content-Type': 'application/json;charset=utf-8'},
			transformRequest: [function (data) {
				return JSON.stringify(data);
			}]
		},
		config
	)
);

// request拦截器
jsonService.interceptors.request.use(interceptors.request.config, interceptors.request.error);
// respone拦截器
jsonService.interceptors.response.use(interceptors.response.response, interceptors.response.error);
export const fetch = jsonService;




const fileService = axios.create(
	objectMerge(
		{
			headers: {'Content-Type': 'application/x-www-form-urlencoded'},
			transformRequest: [function (data) {
				return data
			}],
			timeout: 300000
		},
		config
	)
);
fileService.interceptors.response.use(interceptors.response.response, interceptors.response.error);

export const upload = fileService;


